using System;
using System.Collections.Generic;
using System.Linq;

public class Order
{
    public int OrderId { get; set; }
    public string CustomerName { get; set; }
    public decimal TotalAmount { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType()) return false;
        Order other = (Order)obj;
        return OrderId == other.OrderId;
    }

    public override int GetHashCode()
    {
        return OrderId;
    }

    public override string ToString()
    {
        return $"Order ID: {OrderId}, Customer: {CustomerName}, Total: {TotalAmount}";
    }
}

public class OrderDetails
{
    public int OrderId { get; set; }
    public string ProductName { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType()) return false;
        OrderDetails other = (OrderDetails)obj;
        return OrderId == other.OrderId &&
               Quantity == other.Quantity &&
               UnitPrice == other.UnitPrice;
    }

    public override int GetHashCode()
    {
        return OrderId;
    }

    public override string ToString()
    {
        return $"Order ID: {OrderId}, Product: {ProductName}, Quantity: {Quantity}, Unit Price: {UnitPrice}";
    }
}

public class OrderService
{
    private List<Order> orders = new List<Order>();
    private List<OrderDetails> orderDetails = new List<OrderDetails>();

    public void AddOrder(Order order)
    {
        if (orders.Any(o => o.OrderId == order.OrderId))
        {
            throw new Exception("Order ID already exists.");
        }
        orders.Add(order);
    }

    public void RemoveOrder(int orderId)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new Exception("Order not found.");
        }
        orders.Remove(order);
    }

    public void UpdateOrder(Order order)
    {
        var existingOrder = orders.FirstOrDefault(o => o.OrderId == order.OrderId);
        if (existingOrder == null)
        {
            throw new Exception("Order not found.");
        }
        var index = orders.IndexOf(existingOrder);
        orders[index] = order;
    }

    public List<Order> FindOrders(Func<Order, bool> predicate)
    {
        return orders.Where(predicate).ToList();
    }

    public List<Order> OrderOrders(Func<Order, int> predicate)
    {
        return orders.OrderBy(predicate).ToList();
    }

    public void Export(string filePath)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
        using (TextWriter writer = new StreamWriter(filePath))
        {
            serializer.Serialize(writer, orders);
        }
    }

    public void Import(string filePath)
    {
        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException("File not found.", filePath);
        }

        XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
        using (TextReader reader = new StreamReader(filePath))
        {
            List<Order> importedOrders = (List<Order>)serializer.Deserialize(reader);
            orders.AddRange(importedOrders);
        }
    }

}

class Program
{
    static void Main(string[] args)
    {
        OrderService orderService = new OrderService();

        orderService.AddOrder(new Order { OrderId = 1, CustomerName = "Alice", TotalAmount = 100 });
        orderService.AddOrder(new Order { OrderId = 2, CustomerName = "Bob", TotalAmount = 200 });

        var orders = orderService.FindOrders(o => o.TotalAmount > 150);
        foreach (var order in orders)
        {
            Console.WriteLine(order);
        }

        try
        {
            orderService.UpdateOrder(new Order { OrderId = 1, CustomerName = "Alice Smith", TotalAmount = 120 });
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        try
        {
            orderService.RemoveOrder(2);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        var sortedOrders = orderService.OrderOrders(o => o.OrderId);
        foreach (var order in sortedOrders)
        {
            Console.WriteLine(order);
        }
    }
}
